package com.cloud.wyf.Util;

import com.cloud.wyf.Entity.Middel;
import com.cloud.wyf.Entity.Ordinary;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class POIUtil {
    public static List<Middel> excelLeader(MultipartFile file) {
        List<Middel> list = new ArrayList<>();
        Middel score = null;
        try {
            //1. 创建一个 workbook 对象
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                XSSFSheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        continue;//跳过标题行
                    }
                    //6. 获取行
                    XSSFRow row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    score = new Middel();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        XSSFCell cell = row.getCell(k);
                        switch (cell.getCellType()) {
                            // 类型是 String 进入此 case 块
                            case Cell.CELL_TYPE_STRING:
                                switch (k) {
                                    case 0:
                                        score.setUid(cell.getStringCellValue());
                                        break;
                                    case 1:
                                        score.setRealname(cell.getStringCellValue());
                                        break;
                                    case 2:
                                        score.setPart(cell.getStringCellValue());
                                        break;
                                    case 3:
                                        score.setPosition(cell.getStringCellValue());
                                        break;
                                    case 4:
                                        score.setLevel(cell.getStringCellValue());
                                        break;
                                }
                                break;
                            // 类型是 Date或者数字 进入此 case 块
                        }
                    }
                    // 最后将解析后的数据添加到员工集合中
                    list.add(score);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Ordinary> excelOrdinary(MultipartFile file) {
        Double d=new Double(0);
        List<Ordinary> list = new ArrayList<>();
        Ordinary score = null;
        try {
            //1. 创建一个 workbook 对象
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                XSSFSheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        continue;//跳过标题行
                    }
                    //6. 获取行
                    XSSFRow row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    score = new Ordinary();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        XSSFCell cell = row.getCell(k);
                        switch (cell.getCellType()) {
                            // 类型是 String 进入此 case 块
                            case Cell.CELL_TYPE_STRING:
                                switch (k) {
                                    case 0:
                                        score.setUid(cell.getStringCellValue());
                                        break;
                                    case 1:
                                        score.setRealname(cell.getStringCellValue());
                                        break;
                                    case 2:
                                        score.setPart(cell.getStringCellValue());
                                        break;
                                }
                            // 类型是 Date或者数字 进入此 case 块
                            default: {
                                switch (k) {
                                    case 3:
                                        score.setVote((int)(cell.getNumericCellValue()));
                                        break;
                                }
                            }
                            break;
                        }
                    }
                    // 最后将解析后的数据添加到员工集合中
                    list.add(score);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Middel> excelMiddel(MultipartFile file){
        List<Middel> list = new ArrayList<>();
        Middel score = null;
        try {
            //1. 创建一个 workbook 对象
            XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
            //2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                //3. 获取表单
                XSSFSheet sheet = workbook.getSheetAt(i);
                //4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    //5. 跳过标题行
                    if (j == 0) {
                        continue;//跳过标题行
                    }
                    //6. 获取行
                    XSSFRow row = sheet.getRow(j);
                    if (row == null) {
                        continue;//防止数据中间有空行
                    }
                    //7. 获取列数
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    score = new Middel();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        XSSFCell cell = row.getCell(k);
                        switch (cell.getCellType()) {
                            // 类型是 String 进入此 case 块
                            case Cell.CELL_TYPE_STRING:
                                switch (k) {
                                    case 0:
                                        score.setUid(cell.getStringCellValue());
                                        break;
                                    case 4:
                                        score.setPart(cell.getStringCellValue());
                                        break;
                                    case 5:
                                        score.setPosition(cell.getStringCellValue());
                                        score.setLevel(cell.getStringCellValue());
                                        break;
                                }
                                break;
                        }
                    }
                    // 最后将解析后的数据添加到员工集合中
                    list.add(score);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}

